﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace LVYAN.Utils
{
    public class WinWechat
    {


        public List<string> GetPathList()
        {
            List<string> PathList = new List<string> { };
            PathList.Add(@"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe");
            PathList.Add(@"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe");
            PathList.Add(@"C:\Program Files\Tencent\WeChat\WeChat.exe");
            PathList.Add(@"D:\Program Files\Tencent\WeChat\WeChat.exe");
            return PathList;
        }

        /// <summary>
        /// 获取微信进场ID
        /// </summary>
        /// <returns></returns>
        public int GetWechatProcessID()
        {
            WinProcess winProcess = new WinProcess();
            var Exists = winProcess.Exists(WinProcessType.WeChat.ToString());
            while (!Exists)
            {
                winProcess.Start($"{WinProcessType.WeChat.ToString()}\\" + WinProcessType.WeChat.ToString() + ".exe", GetPathList());
                Thread.Sleep(2000);
                Console.WriteLine("未启动微信");
                Exists = winProcess.Exists(WinProcessType.WeChat.ToString());
            }
            var Count = winProcess.GetProcessCountByName(WinProcessType.WeChat.ToString());
            while (Count > 1)
            {
                Console.WriteLine($"微信启动的数量：{Count}");
                winProcess.Kill(WinProcessType.WeChat.ToString());
                Console.WriteLine("微信只可启动一个");
                Thread.Sleep(2000);
                Count = winProcess.GetProcessCountByName(WinProcessType.WeChat.ToString());
            }
            var Id = winProcess.GetProcessIDByName(WinProcessType.WeChat.ToString());
            return Id;
        }
        /// <summary>
        /// 微信进场ID
        /// </summary>
        /// <returns></returns>
        public int StartWechatProcessID()
        {
            WinProcess winProcess = new WinProcess();
            var Id = winProcess.Start($"{WinProcessType.WeChat.ToString()}\\" + WinProcessType.WeChat.ToString() + ".exe", GetPathList()).Id;
            return Id;
        }
        /// <summary>
        /// 关闭所有返回微信进场ID
        /// </summary>
        /// <returns></returns>
        public int KillResetStartWechatProcessID()
        {
            WinProcess winProcess = new WinProcess();
            winProcess.KillALL(WinProcessType.WeChat.ToString());
            var Id = winProcess.Start($"{WinProcessType.WeChat.ToString()}\\" + WinProcessType.WeChat.ToString() + ".exe", GetPathList()).Id;
            return Id;
        }
    }
}
